package com.syauto.oa;

import android.app.AlertDialog;
import android.app.TabActivity;
import android.app.Notification;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Window;
import android.widget.RadioButton;
import android.widget.CompoundButton;
import android.widget.TabHost;
import android.content.DialogInterface;
import android.content.Intent;
import com.syauto.oa.models.UserInfo;

public class MainTabActivity extends TabActivity implements
		CompoundButton.OnCheckedChangeListener {

	Notification notfin;
	private TabHost mHost;
	private Intent mMBlogIntent;
	private Intent mMoreIntent;
	private Intent mSearchIntent;
	private Intent mUserInfoIntent;
	private Intent mInfoIntent;
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			this.showTips();
			return false;
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		System.exit(0);
	}

	public MainTabActivity() {
		
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		this.setContentView(R.layout.maintabs);
		mMBlogIntent = new Intent(this, MainActivity.class);
		mMoreIntent = new Intent(this, UserInfo.class);
		mInfoIntent = new Intent(this, SalesCalculatorActivity.class);
		initRadios();
		setupIntent();
	}

	private void initRadios() {
		((RadioButton) findViewById(R.id.radioButton01))
				.setOnCheckedChangeListener(this);

		((RadioButton) findViewById(R.id.radioButton02))
				.setOnCheckedChangeListener(this);

		((RadioButton) findViewById(R.id.radioButton03))
				.setOnCheckedChangeListener(this);

		((RadioButton) findViewById(R.id.radioButton04))
				.setOnCheckedChangeListener(this);

		((RadioButton) findViewById(R.id.radioButton05))
				.setOnCheckedChangeListener(this);
	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		if (isChecked) {
			switch (buttonView.getId()) {
			case R.id.radioButton01:
				this.mHost.setCurrentTabByTag("mblog_tab");
				break;
			case R.id.radioButton02:
				this.mHost.setCurrentTabByTag("message_tab");
				break;
			case R.id.radioButton03:
				this.mHost.setCurrentTabByTag("userinfo_tab");
				break;
			case R.id.radioButton04:
				this.mHost.setCurrentTabByTag("search_tab");
				break;
			case R.id.radioButton05:
				this.mHost.setCurrentTabByTag("more_tab");
				break;
			}
		}
	}

	private void setupIntent() {
		this.mHost = getTabHost();
		TabHost localTabHost = this.mHost;

		localTabHost.addTab(buildTabSpec("mblog_tab", R.string.main_home,
				R.drawable.icon_1_n, this.mMBlogIntent));

		localTabHost.addTab(buildTabSpec("message_tab", R.string.main_news,
				R.drawable.icon_2_n, this.mMoreIntent));

		localTabHost.addTab(buildTabSpec("userinfo_tab", R.string.main_my_info,
				R.drawable.icon_3_n, this.mUserInfoIntent));

		localTabHost.addTab(buildTabSpec("search_tab", R.string.menu_search,
				R.drawable.icon_4_n, this.mSearchIntent));

		localTabHost.addTab(buildTabSpec("more_tab", R.string.more,
				R.drawable.icon_5_n, this.mInfoIntent));

	}

	private TabHost.TabSpec buildTabSpec(String tag, int resLabel, int resIcon,
			final Intent content) {
		return this.mHost.newTabSpec(tag).setIndicator(getString(resLabel),
				getResources().getDrawable(resIcon)).setContent(content);
	}

	public void showTips()
	{
		AlertDialog.Builder builder = new AlertDialog.Builder(MainTabActivity.this);
		builder.setMessage(R.string.exitAppMessage).setCancelable(
				false).setPositiveButton(R.string.strYes,
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						MainTabActivity.this.finish();
					}
				}).setNegativeButton(R.string.strNo,
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						return;
					}
				});
		AlertDialog alert = builder.create();
		alert.show();
	}
}
